package com.cn.wanxi.servlet.back.facebook;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.FacebookModel;
import com.cn.wanxi.service.IFacebookService;
import com.cn.wanxi.service.impl.FacebookServiceImpl;
import com.cn.wanxi.util.DataEncoding;
import com.cn.wanxi.util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/facebook/info")
public class FacebookInfoServlet extends HttpServlet {
    IFacebookService facebookService = new FacebookServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //用户详情
        String fid = req.getParameter("id");
        int id = 0;
        if (!"".equals(fid))
            id = Integer.parseInt(fid);
        FacebookModel facebookModel = facebookService.getFacebook(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("facebook",facebookModel);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //留言模糊查询
        String title = req.getParameter("title");
        String start_date = req.getParameter("start_date");
        String end_date = req.getParameter("end_date");
        String email = req.getParameter("email");
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        //SQL语句拼接
        String sql = "select * from facebook where 1=1 ";
        if (!"".equals(title))
            sql+="and title like '%"+title+"%'";
        if (!"".equals(email))
            sql+="and email='"+email+"'";
        if (!"".equals(start_date))
            sql+="and date>='"+start_date+"'";
        if (!"".equals(end_date))
            sql+="and date<='"+end_date+"'";
        long total = JDBCUtils.getCount(sql.replace("*","count(*)"));
        System.out.println(sql + ','+ total);
        sql+=" limit "+ (page-1)*size +","+size;
        List<FacebookModel> facebookList = facebookService.searchFacebook(sql);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("facebook",facebookList);
        jsonObject.put("total",total);
        resp.getWriter().print(jsonObject);
    }
}
